package com.sxfq.loansupermarket.backsystem.modular.arrival.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.IArrearsCustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/arrearsCustomer")
public class ArrearsCustomerController extends BaseController {

    private String PREFIX = "/balance/";
    @Autowired
    IArrearsCustomerService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;
    /**
     * 跳转到欠费患者页面
     */
    @RequestMapping("")
    public String arrearsCustomer(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        model.addAttribute("sttleAccountsTime",  redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"SttleAccountsTime",0));
        return PREFIX + "arrearsCustomer.html";
    }

    /**
     * 欠费患者管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchCustomerInfo info, HttpServletRequest request) {
        Page<CustomerInfo> page = new PageFactory<CustomerInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<CustomerInfo> list = service.selectPageListByQueryGetMap(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.selectPageListByQueryGetMap(page, info);
            }
            page.setTotal(service.getQueryPagesCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping("/sumarrearsCustomer")
    @ResponseBody
    public HashMap sumarrearsCustomer(@RequestParam(required = false) String reach, @RequestParam(required = false) String startArrivalTime, @RequestParam(required = false) String endArrivalTime) {
        String tableName=redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"customer_info",0)+"";
        String tableName1=redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"deal_info",0)+"";
        String sql = " " +
                " select sum(tt.account_arrears) as borrowAmount_sum from " +
                "(select t1.account_arrears" +
                " from "+tableName+" t1\n" +
                " left join "+tableName1+" t2 on (t1.reservations_id=t2.reservations_id)\n" +
                " where t1.account_arrears>0 and t1.arrival_time is not null\n";
        if (StringUtil.isNotEmpty(startArrivalTime)) {
            sql += " and t1.arrival_time >= '" + startArrivalTime + "'";
        }
        if (StringUtil.isNotEmpty(endArrivalTime)) {
            sql += " and t1.arrival_time <= '" + endArrivalTime + "'";
        }
        if (StringUtil.isNotEmpty(reach)) {
            sql += "  and (t1.customer_name like CONCAT('%','" + reach + "','%') or t1.customer_phone like CONCAT('%','" + reach + "','%') " +
                    " or t1.wechat_number like CONCAT('%','" + reach + "','%') or t1.medical_record_number like CONCAT('%','" + reach + "','%') " +
                    " or t1.szm like CONCAT('%','" + reach + "','%'))";
        }
        sql+=" group by t1.reservations_id) tt";
        Map<String, Object> result = sqlMapper.selectOne(sql.toString());
        HashMap map = new HashMap();
        if (result != null) {
            map.put("borrowAmount_sum", result.get("borrowAmount_sum"));
        } else {
            map.put("borrowAmount_sum", "0");
        }
        return map;
    }
}